package com.aaa.service.mht.impl;


import com.aaa.dao.mht.TMhtSubMapper;
import com.aaa.entity.MyPage;
import com.aaa.entity.mht.TMhtSub;
import com.aaa.service.mht.TMhtSubService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (TMhtSub)表服务实现类
 *
 */
@Service
public class TMhtSubServiceImpl implements TMhtSubService {
   @Autowired
   private TMhtSubMapper tMhtSubMapper;

   @Override
   public void dealFile(MultipartFile file) {

      // 如果试题已经存在就不让再导入了
      int i = tMhtSubMapper.selectCount(null);
      if (i > 0) {
         return;
      } else {
         // 将excel中的数据导入到数据库中
//      InputStream is = null;
//         String fileUrl="https://yuyonglitest.oss-cn-beijing.aliyuncs.com/%E9%A2%98%E7%9B%AE%E6%B8%85%E5%8D%952.xlsx";
//      try {
//         is = new URL(fileUrl).openStream();
//      } catch (IOException e) {
//         e.printStackTrace();
//      }
         InputStream is = null;
         try {
            is = file.getInputStream();
         } catch (IOException e) {
            e.printStackTrace();
         }
         //1.进行读取数据，TMhtSub是我的pojo类，
         //2.new FileListener(fileService)这个是监听器，主要用来读取数据的
         //3.特别注意的是fileService这个service，我上面有注入进去 @Autowired/@Resource，切记不要new会报错
         //EasyExcel.read(is, TMhtSub.class, new FileListener(fileService)).sheet("MHT性格测试").doRead();
         EasyExcel.read(is, TMhtSub.class, new AnalysisEventListener<TMhtSub>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 100;
            /**
             * 临时存储
             */
            private List<TMhtSub> cachedDataList = new ArrayList<>();

            @Override
            public void invoke(TMhtSub data, AnalysisContext context) {
               cachedDataList.add(data);
               if (cachedDataList.size() >= BATCH_COUNT) {
                  saveData();
                  // 存储完成清理 list
                  cachedDataList = new ArrayList<>();
               }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
               saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
//            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
//            log.info("存储数据库成功！");
               save(cachedDataList);
            }
         }).sheet("MHT量表").doRead();
      }
   }

   @Override
   public void save(List<TMhtSub> list) {
      System.out.println("list = ===================================================================" + list);
      tMhtSubMapper.insertMhtSubjects(list);
   }

   @Override
   public PageInfo getAll(MyPage myPage) {

      PageHelper.startPage(myPage.getPage(), myPage.getLimit());

      return new PageInfo(tMhtSubMapper.selectAll());
   }

   @Override
   public boolean updEdit(TMhtSub tMhtSub) {
      int i = -1;
//      添加的id为null这就是添加
      if (tMhtSub.getId() == null) {
         i = tMhtSubMapper.insert(tMhtSub);
      } else {
         //      根据主键更新属性不为null的值
         i = tMhtSubMapper.updateByPrimaryKeySelective(tMhtSub);
      }
      return i > 0 ? true : false;

   }

   @Override
   public boolean del(Integer id, Integer[] mhtno) {
//      删除题目
//      int delete = tMhtSubMapper.delete(tMhtSub);
//      return delete>0?true:false;
      List<Map<String, Object>> zuoti = tMhtSubMapper.zuoti(mhtno);
      if (zuoti.size()!= 0) {
         return false;
      } else {
         boolean deletemht = tMhtSubMapper.deletemht(id);
         System.out.println("单条删除deletemht = " + deletemht);
         return true;
      }
   }

   @Override
   public PageInfo findAllSub(MyPage myPage, TMhtSub tMhtSub) {
      PageHelper.startPage(myPage.getPage(), myPage.getLimit());
      List<TMhtSub> allSub = tMhtSubMapper.findAllSub(tMhtSub);
      PageInfo pageInfo = new PageInfo(allSub);
      return pageInfo;
   }
/*
* 批量删除
* */
   @Override
   public boolean delsdsbath(Integer[] id, Integer[] mhtno) {
      List<Map<String, Object>> zuotiduo = tMhtSubMapper.zuotiduo(mhtno);
      if (zuotiduo.size()!= 0) {
         return false;
      } else {
         int delmhtbath = tMhtSubMapper.delmhtbath(id);
         return true;
      }
   }
}

